<?php

/**
 * CommissionController
 * 
 * @author Radovan Dvorsky
 * @version 20100512
 */

require_once 'Zend/Controller/Action.php';

class CommissionController extends Zend_Controller_Action {
	
	public function init(){
		$contextSwitch = $this->_helper->getHelper('AjaxContext');
        $contextSwitch->addActionContext('show-commission', 'html')
        ->addActionContext('delete', 'html')
        ->addActionContext('remove-professor', 'html')
        ->addActionContext('add-professor', 'html')
        ->addActionContext('set-role', 'html')
        ->initContext();
	}
	
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		
		$commissionsMapper = new Db_Mapper_Commission();
				
		$cache = Zend_Registry::get('cache');		
		$cacheName = My_Support::getCacheName($this,'fetchAll');
		
		/*
		 * Ak existuje nacitaj z cache
		 */
		if(!($commissions = $cache->load($cacheName))){

			$select = $commissionsMapper->getTable()->select();
			$select->order('commission.name');
			$commissions = $commissionsMapper->fetchAll($select);									
			$cache->save($commissions,$cacheName,array(get_class($this)));
		}
		
		//$this->view->form = $form;
		$this->view->commissions = $commissions;
			
	}
	
	public function showCommissionAction(){
		
		$form = new Form_Commission_AddProfessor($this->view->url(array(
				'controller' => 'commission',
				'action' => 'add-professor',
				'format' => 'html'							
				)
			),
			'table.commissionTable'
		);
		
		$cid = (int)$this->getRequest()->getParam('cid');
		$commissionMapper = new Db_Mapper_Commission();
		
		$cache = Zend_Registry::get('cache');		
		
				
		$cacheName = My_Support::getCacheName($this,'show-commission-'.(int)$cid);
				
		if(!($commissionModel = $cache->load($cacheName))){			
			$select = $commissionMapper->getTable()->select()->order('professor.lastname');
			
			$commissionModelCid = new Db_Model_Commission(array('cid' => $cid ));	
			$commissionModel = $commissionMapper->fetchWithDependentRowset(
				'Db_DbTable_Professor',
				'Db_DbTable_ProfessorCommission',
				$commissionModelCid,
				$select
			);			
			$cache->save($commissionModel,$cacheName,array(get_class($this)));
		}	
		
		$this->view->form = $form;
		$this->view->commission = $commissionModel;
	}
	
	public function addProfessorAction(){
		
		$cid = (int)$this->getRequest()->getParam('cid');
		$pid = (int)$this->getRequest()->getParam('pid');
		
		try{
			$data = array(
				'pid' => $pid,
				'cid' => $cid
			);
			
			$this->view->data = $data;
			$professorMapper = new Db_Mapper_Professor();
			$this->view->professor = $professorMapper->find(new Db_Model_Professor(array('pid' => $pid)));
			
			$profCommMapper = new Db_Mapper_ProfessorCommission();
			$this->view->insert = $profCommMapper->save(new Db_Model_ProfessorCommission($data),true);
				
			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'show-commission-'.$cid));

			
			//$this->view->message = '<p class="success">Profesor pridaný</p>';
					
		} catch(Zend_Db_Exception $e){
					
			$log = Zend_Registry::get('log');    		
    		$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
			
			$this->view->message = '<p class="error">Profesora sa nepodarilo pridať</p>';
		}
		
	}
	
	public function addAction(){
		$addForm = new Form_Commission_Add();
		
		$this->view->form = $addForm;
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($addForm->isValid($formData)){				

				try {
					$professorsArrayList = array();
					
					foreach($addForm->getValue('professors') as $pid){
						$professorsArrayList[] = new Db_Model_Professor(array('pid' => $pid));
					}	
					
					$data = array(
						'name' => $addForm->getValue('name'),
						'professors' => $professorsArrayList
					);
					
					
					$commissionMapper = new Db_Mapper_Commission();
					$commissionModel = new Db_Model_Commission($data);
					$commissionMapper->save($commissionModel,true);
										
					$cache = Zend_Registry::get('cache');					
					$cache->remove(My_Support::getCacheName($this,'fetchAll'));
					
					$this->_redirect('/commission/');
					
				} catch(Zend_Exception $e){
					$log = Zend_Registry::get('log');    		
    				$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
					$this->view->message = '<p class="error">Komisiu sa nepodarilo pridať</p>';
				}				
			}			
		}
	}
	
	public function deleteAction(){
		
		/*
		 * Ziskaj cid komisie z url
		 */
		$cid = $this->getRequest()->getParam('cid');
		
		try{
			
						
			/*
			 * Najdi komisiu v db podla cid a odstran
			 */
			$commissionMapper = new Db_Mapper_Commission();
			$commissionMapper->delete(new Db_Model_Commission(array('cid' => $cid)));
			
			
			/*
			 * Vymaz cache
			 */
			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'fetchAll'));
			$cache->remove('harmonograms');
			
			/**
			 * TODO Vymyslet neco rozumnejsi
			 * 
			 * Ak je odstranena komisia, ktora uz je pridelena aj v harmonograme, tak
			 * zmazanie tejto komisie zmaze automaticky(table relations) aj vsetky harmonogramy
			 * v ktorych je komisia pridelena, vzhladom na to moze vzniknut problem zo zoradanim
			 * harmonogramov 
			 */
			$harmonogramModel = new Db_Harmonogram();
			$harmonogramModel->orderHarmonograms();
			
			$this->view->message = '<p class="success">Komisia odstránená</p>';
			
		} catch (Zend_Db_Exception $e){
			$log = Zend_Registry::get('log');    		
    		$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
    		$this->view->message = '<p class="error">Komisiu sa nepodarilo odstrániť</p>';
		}		
	}
	
	public function removeProfessorAction(){
		
		$params = $this->getRequest()->getParams();
						
		try{
		
			$professor = new Db_ProfessorCommission();		
			
			$adapter = $professor->getAdapter();
			
			$where = $adapter->quoteInto('pid = ? ', $params['pid'],Zend_Db::INT_TYPE) . 
					 $adapter->quoteInto('AND cid = ?',$params['cid'],Zend_Db::INT_TYPE);
					
			$professor->delete($where);
			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'show-commission-'.(int)$params['cid']));			
			
			//$this->view->message = '<p class="success">Profesor odstránený</p>';
			
		} catch(Zend_Db_Exception $e){
			
			$log = Zend_Registry::get('log');    		
    		$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
    		
    		$this->view->message = '<p class="error">Profesora sa nepodarilo odstránit</p>';
		}		
	}
	
	public function setRoleAction(){
		
		$params = $this->getRequest()->getParams();
				
		try{
			
			$professorCommissionModel = new Db_ProfessorCommission();
			
			$adapter = $professorCommissionModel->getAdapter();
			
			$where = $adapter->quoteInto('cid = ? AND ',$params['cid'],Zend_Db::INT_TYPE)
			. $adapter->quoteInto('pid = ?',$params['pid'],Zend_Db::INT_TYPE);
			
			$professorCommissionModel->update(
				array(
					'role' => $params['role' . $params['cid'] . $params['pid']],
				),
				$where
			);

			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'fetchAll'));
			
			//$this->view->message = '<p class="success">Funkcia pridelená</p>';
			
		} catch (Zend_Db_Exception $e){
			$log = Zend_Registry::get('log');    		
    		$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
    		
    		$this->view->message = '<p class="error">Funkcie profesora sa nepodarila priradiť</p>';
		}
	}
}